package com.cqupt.controller;

import com.cqupt.common.Result;
import com.cqupt.pojo.TbUser;
import com.cqupt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String login() {
        return "login";
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public Result login_submit(TbUser user, HttpSession session) {
        Result result = new Result();

        TbUser tbuser = userService.queryUser(user);
        if(tbuser==null) {
            result.setCode(1);
            result.setMsg("用户名不存在");
        } else {
            if(tbuser.getUserPwd().equals(user.getUserPwd())) {
                session.setAttribute("user",tbuser);
                result.setCode(0);
                result.setMsg("登录成功");
            } else {
                result.setCode(2);
                result.setMsg("用户名或密码输入有误");
            }
        }
        return result;
    }

}
